import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;

//TODO Factor in the appearance of images being determined by its top left corner 
//(Human ship first appearing and Buggers showing off screen)
//TODO Trim rectangle hit boxes (people hate dying when only slight touch w/ Bugger)
//TODO Display time in paint method

// Note: "Screen" refers to a number that is assigned to each Bugger set and Human
// So the focused screen does not have a fixed screen number

public class Board extends JPanel {

	private final int TIMER_INCREMENT_STARS = 500;

	private Human human;
	private ArrayList<Bugger> buggers;

	private Timer repaintTimer;

	static boolean accelerating; // True indicates the player is accelerating
									// forward (Buggers move faster towards you)

	public Board(Human human, ArrayList<Bugger> buggers) {
		addKeyListener(new TAdapter());
		setFocusable(true);
		setBackground(Color.BLACK);
		setDoubleBuffered(true);
		setSize(EndersGame.WIDTH, EndersGame.HEIGHT);

		this.human = human;
		this.buggers = buggers;

		repaintTimer = new Timer(EndersGame.TIMER_INCREMENTS_REPAINT,
				repaintController);
		repaintTimer.start();
	}

	public void setHuman(Human human) {
		this.human = human;
	}

	public void setBuggers(ArrayList<Bugger> buggers) {
		this.buggers = buggers;
	}

	public Human getHuman() {
		return human;
	}

	public ArrayList<Bugger> getBuggers() {
		return buggers;
	}

	public void paint(Graphics g) {
		super.paint(g);

		Graphics2D g2d = (Graphics2D) g;

		Font font = new Font("Helvetica", Font.BOLD, 14);

		if (!EndersGame.initial) {
			if (EndersGame.inGame) {

				for (Bugger i : buggers) {
					g2d.drawImage(i.getImage(), i.getX(), i.getY(), this);
				}

				g2d.drawImage(human.getImage(), human.getX(), human.getY(),
						this);

				g2d.setColor(Color.WHITE);
				g2d.drawString("Enemies Dodged: " + EndersGame.dodgedCounter,
						5, 15);
			} else {
				String msg = "Game Over";
				String dodged = "Enemies Dodged: " + EndersGame.dodgedCounter;
				String retryStatement = "Press Enter to Restart";
				// Add in message telling to press enter to restart
				FontMetrics metr = this.getFontMetrics(font);

				g.setColor(Color.WHITE);
				g.setFont(font);
				g.drawString(msg,
						(EndersGame.WINDOW_WIDTH - metr.stringWidth(msg)) / 2,
						EndersGame.WINDOW_HEIGHT / 2);
				g.drawString(
						dodged,
						(EndersGame.WINDOW_WIDTH - metr.stringWidth(dodged)) / 2,
						EndersGame.WINDOW_HEIGHT / 2 + 20);
				g.drawString(retryStatement, (EndersGame.WINDOW_WIDTH - metr
						.stringWidth(retryStatement)) / 2,
						EndersGame.WINDOW_HEIGHT / 2 + 40);
			}
		} else {
			g.setColor(Color.WHITE);
			g.setFont(font);

			FontMetrics metr = this.getFontMetrics(font);

			String[] initialMsg = new String[4];
			initialMsg[0] = "Use arrow keys to control your ships";
			initialMsg[1] = "Press 1, 2 and 3 to switch between ships";
			initialMsg[2] = "Press space bar to accelerate";
			initialMsg[3] = "Press Enter to start the game";

			// System.out.println("Printed an initial string");

			for (int i = 0; i < initialMsg.length; i++) {
				// System.out.println("Created an initial string");
				g.drawString(initialMsg[i], (EndersGame.WINDOW_WIDTH - metr
						.stringWidth(initialMsg[i])) / 2,
						EndersGame.WINDOW_HEIGHT / 2 + 20 * (i + 1));
			}
		}

		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	// Determine which Buggers are visible and remove them as necessary, then
	// repaint.
	private ActionListener repaintController = new ActionListener() {
		public void actionPerformed(ActionEvent ae) {
			if (Board.accelerating == true) {
				for (Bugger i : buggers) {
					i.accelerate();
				}
			} else {
				for (Bugger i : buggers) {
					i.setToNormalSpeed();
				}
			}
			human.move();
			repaint();
		}
	};

	private class TAdapter extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			human.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			human.keyPressed(e);
		}
	}
}
